<template>
	<div class="aid">
		<div class="base">
			<p class="fixed-top">
				{{ (!currentID ? '添加' : '修改')+$route.meta.name }}
				<a-icon @click="closeFunction" type="close" />
			</p>
			<div class="main" id="new_message">
				<a-form :form="form">
					<a-form-item label="活动类型">
						<a-tree-select
							:allowClear="true"
							v-decorator="['ActivityTypeID', { rules: [{ required: true, message: '请选择活动类型' }] }]"
							:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
							:tree-data="ActivityTypeList"
							:replace-fields="replaceFields"
						></a-tree-select>
					</a-form-item>
					<a-form-item label="分值">
						<a-input type="number" :min="0" @blur="changeVal" v-decorator="['Score', { rules: [{ required: true, message: '请输入分值' }] }]" />
					</a-form-item>
					<a-form-item class="fixed-bottom">
						<a-button type="primary" @click="handleSubmit">{{ !currentID ? '确认添加' : '确认修改' }}</a-button>
						<a-button style="margin-left: 10px;" @click="closeFunction">取消</a-button>
					</a-form-item>
				</a-form>
			</div> 
		</div>
	</div>
</template>

<script>
import moment from 'moment';
import { ManageExtraScoreItem,GetUserList } from '@/api/follow';
export default {
	props: {
		text: Object,
		ActivityTypeList:Array
	},
	watch: {
		text: function(text) {
			if (JSON.stringify(text) == '{}') {
				this.currentID = ''
			} else {
				let that = this;
				that.currentID = text.ID;
				that.userList = [{Name:text.UserName,UserNo: text.UserNo}]
				setTimeout(function() {
					that.form.setFieldsValue({
						ActivityTypeID: text.ActivityTypeID,
						Score:text.Score
					});
				}, 300);
			}
		}
	},
	data() {
		return {
			loading: false,
			formLayout: 'horizontal',
			form: this.$form.createForm(this, {
				name: 'coordinated'
			}),
			currentID: '',
			replaceFields: {
				children: 'children',
				title: 'Name',
				key: 'ID',
				value: 'ID'
			},
		};
	},
	created() {},
	methods: {
		moment,
		changeVal(e){
			if(Number(e.target._value)<0){
				let that = this
				setTimeout(function() {
					that.form.setFieldsValue({
						Score:0
					});
				}, 30);
			}
		},
		filterOption(input, option) {
			return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0;
		},
		async fetchUser(value) {
			if (value && value.length > 1) {
				let data = {};
				data.key = value;
				data.top = 20;
				let res = await GetUserList(data);
				this.userList = res.data.data;
			}
		},
		closeFunction(data) {
			this.form.resetFields();
			this.$emit('closeFun', data);
		},
		handleSubmit(e) {
			this.form.validateFields(async (err, values) => {
				if (!err) {
					let data = {};
					data = values
					let res = {};
					data.ExtraScoreID = this.$route.query.ID
					if (!this.currentID) {
						res = await ManageExtraScoreItem('DoAdd',data);
					} else {
						data.ID = this.currentID;
						res = await ManageExtraScoreItem('DoUpdate',data);
					}
					if (res.data.code === 0) {
						this.$message.success(res.data.msg);
						this.closeFunction('1');
					} else {
						this.$message.error(res.data.msg);
					}
				}
			});
		}
	}
};
</script>

<style lang="less" scoped>
/deep/.ant-form {
	margin-top: 50px;
	padding-bottom: 50px;
}
/deep/.ant-form-item {
	margin: 20px 50px;
	display: flex;
}
/deep/.ant-form-item-label {
	width: 100px;
	overflow: hidden;
	white-space: nowrap;
	text-overflow: ellipsis;
	text-align: right;
}
/deep/.ant-form-item-control-wrapper {
	flex: 1;
}
/deep/.ant-calendar-picker {
	width: 100%!important;
}
</style>
